package com.qnzw.zjmy.ad.kaku.interstitia;


import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialListener;
import com.qnzw.zjmy.ad.impl.AdRevenueListenerImpl;
import com.qnzw.zjmy.home.ui.enums.TaKuAdPositionEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 插屏广告（半插）
 */
public class TaKuInterstitialAdHome implements ATInterstitialListener {
    private static String TAG = "TaKuInterstitialAdHome";
    private ATInterstitial mInterstitialAd;

    private String token;
    private static volatile TaKuInterstitialAdHome instance;

    private boolean loadFailFlag = false;
    private Activity activity;

    private TaKuInterstitialAdHome() {
    }

    public static synchronized TaKuInterstitialAdHome getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (TaKuInterstitialAdHome.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new TaKuInterstitialAdHome();
                }
            }
        }
        return instance;
    }

    public void loadAd(Context context, String placementId) {
        mInterstitialAd = new ATInterstitial(context, placementId);
        mInterstitialAd.setAdListener(this);
        mInterstitialAd.load();
    }

    public void showAd(Activity activity) {
        this.activity = activity;
        if (ObjectUtil.isNull(mInterstitialAd)) {
            return;
        }
        mInterstitialAd.setAdRevenueListener(new AdRevenueListenerImpl(activity,token, TaKuAdPositionEnum.AD_INTERSTITIAL_POSITION.getKey(), "0"));
        if (mInterstitialAd.isAdReady()) {
            mInterstitialAd.show(activity);
        } else {
            //重新加载
            loadFailFlag = true;
            mInterstitialAd.load();
        }
    }

    @Override
    public void onInterstitialAdLoaded() {
        if (ObjectUtil.isNotNull(activity) && loadFailFlag) {
            mInterstitialAd.show(activity);
            loadFailFlag = false;
        }
    }

    @Override
    public void onInterstitialAdLoadFail(AdError adError) {
        Log.e(TAG, "onInterstitialAdLoadFail:" + adError.getFullErrorInfo());
    }

    @Override
    public void onInterstitialAdClicked(ATAdInfo atAdInfo) {

    }

    @Override
    public void onInterstitialAdShow(ATAdInfo atAdInfo) {
        mInterstitialAd.load();
    }

    @Override
    public void onInterstitialAdClose(ATAdInfo atAdInfo) {

    }

    @Override
    public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {

    }

    @Override
    public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {

    }

    @Override
    public void onInterstitialAdVideoError(AdError adError) {
        Log.e(TAG, "onInterstitialAdVideoError:" + adError.getFullErrorInfo());
    }

    public void setToken(String token) {
        this.token = token;
    }
}
